package com.rgz.interceptorTest1;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class InterceptorTest1 implements HandlerInterceptor {

	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		System.out.println("InterceptorTest1 afterCompletion");

	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
		System.out.println("InterceptorTest1 postHandle");

	}

	@Override
	public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
		System.out.println("InterceptorTest1 preHandle");
		String url = arg0.getRequestURI();
		System.out.println(url);
		if (url.contains("login")) {
			return true;
		}
		Object username = arg0.getSession().getAttribute("username");
		if (username!=null) {
			return true;
		}
		arg0.getRequestDispatcher("user/login.do").forward(arg0, arg1);
		
		
		return false;
	}

}
